package cn.net.withub.dataCollector.common.base.impl;

import cn.net.withub.dataCollector.common.utils.*;
import cn.net.withub.dataCollector.dao.DaoTool;
import cn.net.withub.dataCollector.dao.impl.JdbcTool;
import cn.net.withub.dataCollector.common.base.BaseManager;
import org.apache.log4j.Logger;
import org.hibernate.criterion.CriteriaSpecification;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@SuppressWarnings({"rawtypes","unchecked"})
public class BaseManagerImpl implements BaseManager {
	
	 @Resource
	 protected JdbcTool jdbcTool;
	 /* 
	 @Resource
	 protected XtptJdbcTool xtptJdbcTool;*/
	 @Resource(name="daoTool")
	 protected DaoTool daoTool;
	 @Resource
	 protected CreateNewKey createNewKey;
	 
	 private Logger log = Logger.getLogger(this.getClass());
	 
	 protected Integer statusCode = 404;//状态码
	 protected String message = "系统繁忙,请稍后再试！";// 信息提示用
	 
	@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
	public Object load(Class<?> clazz, String where) {
		String hql = "from " + clazz.getName();
		if (!where.equals("")) {
			hql = hql + (where.trim().toLowerCase().startsWith("where")?where:" where " + where);
		}
		List list = daoTool.createHQLQuery(hql).list();
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
	public Object load(Class<?> clazz, String where,Object[] paramsObj) {
		String hql = "from " + clazz.getName();
		if (!where.equals("")) {
			hql = hql + (where.trim().toLowerCase().startsWith("where")?where:" where " + where);
		}
		List list = daoTool.findList(hql,paramsObj);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
	public Integer getMax(Class<?> clazz, String field,String where) {
		String hql = "select max(CAST("+field+" as integer)) from " + clazz.getName();
		if (!where.equals("")) {
			hql = hql + (where.trim().toLowerCase().startsWith("where")?where:" where " + where);
		}
		Integer max = (Integer)daoTool.getCurrentSession().createQuery(hql).uniqueResult();
		return max;
	}

	@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
	public List findList(Class<?> clazz, String where) {
		String hql = "from " + clazz.getName();
		if (!where.equals("")) {
			if(where.trim().toLowerCase().startsWith("where")){
				hql = hql + " " + where;
			}else if(where.trim().toLowerCase().startsWith("and")){
				hql = hql + " where 1=1 " + where;
			}else{
				hql = hql + " where " + where;
			}
		}
		return daoTool.findAll(hql, null);
	}

	@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
	public List findList(Class<?> clazz, String where,Object[] paramsObj) {
		String hql = "from " + clazz.getName();
		if (!where.equals("")) {
			if(where.trim().toLowerCase().startsWith("where")){
				hql = hql + " " + where;
			}else if(where.trim().toLowerCase().startsWith("and")){
				hql = hql + " where 1=1 " + where;
			}else{
				hql = hql + " where " + where;
			}
		}
		return daoTool.findAll(hql,null,paramsObj);
	}

	@Transactional(readOnly=true,propagation=Propagation.REQUIRED)
	@Cacheable(value="serviceCache")
	public List createSQLListForCache(String sql) throws Exception {
		return daoTool.createSQLQuery(sql).setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
	}

}
